package cn.leida.ruoyi.common.core.domain.entity;

import cn.leida.ruoyi.common.core.domain.BaseEntity;
import com.alibaba.fastjson2.JSONObject;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.util.List;


/**
 * 用户对象 sys_user
 *
 * @author liutietou
 * @since 1.0  2020-12-11
 */
@Data
@Entity
@Table(name = "sys_user")
@DynamicInsert()
@DynamicUpdate()
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private Long userId;

    /**
     * 部门ID
     */
    @Column(name = "dept_id")
    private Long deptId;

    /**
     * 用户账号
     */
    @Column(name = "user_name",length = 40)
    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
    private String userName;

    /**
     * 用户昵称
     */
    @Column(name = "nick_name",length = 40)
    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    private String nickName;

    /**
     * 用户邮箱
     */
    @Column(name = "email",length = 50)
    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    private String email;

    /**
     * 手机号码
     */
    @Column(name = "phone_number",length = 50)
    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    private String phoneNumber;

    /**
     * 用户性别
     */
    @Column(name = "sex",length = 10)
    private String sex;

    /**
     * 用户头像
     */
    @Column(name = "avatar",length = 300)
    private String avatar;

    /**
     * 密码
     */
    @Column(name = "password",length = 100)
  //  @JsonIgnore
 //   @JsonProperty
    private String password;

    /**
     * 帐号状态（0正常 1停用）
     */
    @Column(name = "status",length = 10)
    private String status;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    @Column(name = "del_flag",length = 10)
    private String delFlag;

    /**
     * 最后登录IP
     */
    @Column(name = "login_ip",length = 100)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @Column(name = "login_date",length = 30)
    private String loginDate;

    /**
     * 创建者
     */
    @Column(name = "create_by",length = 64)
    private String createBy;

    /**
     * 创建时间
     */
    @Column(name = "create_time",length = 30)
    private String createTime;

    /**
     * 更新者
     */
    @Column(name = "update_by",length = 64)
    private String updateBy;

    /**
     * 更新时间
     */
    @Column(name = "update_time",length = 30)
    private String updateTime;

    /**
     * 备注
     */
    @Column(name = "remark",length = 500)
    private String remark;

    /**
     * 部门对象
     */
    @Transient
    private SysDept dept;


    /**
     * 角色对象
     */
    @Transient
    private List<SysRole> roles;

    /**
     * 盐加密
     */
    @Transient
    private String salt;

    /**
     * 角色组
     */
    @Transient
    private Long[] roleIds;

    /**
     * 岗位组
     */
    @Transient
    private Long[] postIds;

    @Transient
    private boolean showDoc;

    public SysUser() {
    }

    public SysUser(Long userId) {
        this.userId = userId;
    }

    public boolean isAdmin() {
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }


    public static SysUser from(JSONObject json) {
        SysUser sysUser = json.toJavaObject(SysUser.class);
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(sysUser.getDeptId());
        sysDept.setDeptName(json.getString("deptName"));
        sysDept.setLeader(json.getString("leader"));
        sysUser.setDept(sysDept);
        return sysUser;
    }

    public void initUpdateTime(){
        updateTime = nowTime();
    }
    public void initCreateTime(){
        createTime = nowTime();
    }
    public void initLoginDate(){
        loginDate = nowTime();
    }

    public long deptId(){
        if(deptId!=null){
            return deptId;
        }
        return 0;
    }

}
